<template>
  <el-drawer
    title="保存画像模板"
    size="60%"
    :visible.sync="drawerVisible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="beforeClose"
    custom-class="tag-drawer factory-grp-tmpl-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
          <el-form-item prop="cover" label="覆盖旧模板">
            <el-radio-group v-model="editForm.cover" :disabled="!editForm.editable">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="tmplId" label="覆盖模板名称" v-if="editForm.cover">
            <el-select v-model="editForm.tmplId" filterable @change="chageTmpl" :disabled="!editForm.editable">
              <!-- <el-option value=""></el-option> -->
              <el-option
                v-for="item in tmplList"
                :key="item.tmplId"
                :value="item.tmplId"
                :label="item.tmplNm"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="tmplNm" label="模板名称">
            <el-input placeholder="请输入模板名称" v-model="editForm.tmplNm"></el-input>
          </el-form-item>
          <el-form-item prop="tmplCmnt" label="模板说明">
            <el-input
              type="textarea"
              placeholder="请输入模板说明"
              v-model="editForm.tmplCmnt"
              :rows="3"
              :maxlength="200"
              :show-word-limit="true"
              resize="none"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="beforeClose">取消</el-button>
      <el-button type="primary" size="medium" @click="save">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  data() {
    return {
      drawerVisible: false,
      tmplList: [],
      editForm: {
        editable: true,
        tmplJson: '',
        tmplId: '',
        tmplNm: '',
        tmplCmnt: '',
        cover: false
      },
      editFormRules: {
        tmplId: [{ required: true, message: '覆盖模板不能为空', trigger: 'change' }],
        tmplNm: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
        tmplCmnt: [{ required: true, message: '模板说明不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    showDrawer(tmpl) {
      if (tmpl.tmplId) {
        // 画像编辑
        this.editForm.editable = false
        this.editForm.tmplId = tmpl.tmplId
        this.editForm.tmplJson = tmpl.tmplJson
        this.editForm.tmplId = tmpl.tmplId
        this.editForm.tmplNm = tmpl.tmplNm
        this.editForm.tmplCmnt = tmpl.tmplCmnt
        this.editForm.cover = true
      } else this.editForm.tmplJson = tmpl
      this.getTmplList()
      this.drawerVisible = true
    },
    getTmplList() {
      this.$ajax({
        url: this.$ajax.setUrl('ast/astTagImageTmpl/getAllTemp/1'),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.tmplList = data.data
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    chageTmpl(tmplVal) {
      let tmpItem = this.tmplList.find(item => item.tmplId === tmplVal)
      this.editForm.tmplNm = tmpItem.tmplNm
      this.editForm.tmplCmnt = tmpItem.tmplCmnt
    },
    save() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$ajax({
            url: this.$ajax.setUrl('ast/astTagImageTmpl'),
            method: this.editForm.cover ? 'put' : 'post',
            data: this.$ajax.setData({
              tmplNm: this.editForm.tmplNm,
              tmplCmnt: this.editForm.tmplCmnt,
              tmplJson: this.editForm.tmplJson,
              tmplId: this.editForm.cover ? this.editForm.tmplId : ''
            })
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.data || data.msg)
              this.$emit('callback')
              this.beforeClose()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    clearForm() {
      this.editForm = {
        editable: true,
        tmplJson: '',
        tmplId: '',
        tmplNm: '',
        tmplCmnt: '',
        cover: false
      }
      this.tmplList = []
    },
    beforeClose() {
      this.clearForm()
      this.$refs.editForm.clearValidate()
      this.drawerVisible = false
    }
  }
}
</script>

<style lang="scss"></style>
